<!--
 * @Author: wangming
 * @Date: 2020-09-09 14:04:55
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-05 11:53:26
 * @Description:添加类别按钮
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="420px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>表单隐藏域</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>
    <el-form
      class="pl30 pr30"
      :model="FormData"
      inline
      ref="Form"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="字段类型">
        <el-radio-group v-model="codeType" @change="changeCodeType">
          <el-radio label="1">模型字段</el-radio>
          <el-radio label="2">自定义字段</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="codeType === '1'">
        <el-form-item label="数据字段" prop="key">
          <el-select
            v-model="FormData.key"
            placeholder="请选择"
            size="small"
            class="mb10"
            value-key="code"
            style="width: 220px"
            @change="changeValue"
          >
            <el-option
              v-for="(item, index) in myEntitymodelList"
              :key="index"
              :label="item.name"
              :value="item.code"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div v-if="!!config">
          <el-form-item label="值" prop="value">
            <component
              :tagName="tagName"
              placeholder="请输入"
              style="width: 220px"
              size="small"
              v-model="FormData.value"
              :is="KEYENUM[config.type] || 'elInput'"
              :config="config"
            ></component>
          </el-form-item>
        </div>
      </template>
      <template v-else>
        <el-form-item label="数据字段" prop="key">
          <elInput
            v-model="FormData.key"
            placeholder="请输入"
            style="width: 220px"
          ></elInput>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <elInput
            v-model="FormData.value"
            placeholder="请输入"
            style="width: 220px"
          ></elInput>
        </el-form-item>
      </template>
    </el-form>

    <div slot="footer" class="tr">
      <el-button @click="showVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loading"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { cloneDeep } from "lodash";
import components from "./__components/index.js";
export default {
  components: { ...components },
  data() {
    return {
      tagName: "",
      codeType: "1",
      KEYENUM: {
        // 自动带出字段对应控件
        NUMBER: "NumberInput", // 数值
        INTEGER: "NumberInput", //
        DATE: "DatePicker", // 日期
        TIME: "TimePicker", // 时间
        RELATION_SELECT: "SelectRelation", // 字典
        RELATION_REFERENCE: "SelectRelation",
        BOOL: "BoolSelect",
        STAFF: "StaffSelect",
        DEPARTMENT: "DepartmentSelect",
      },
      loading: false,
      rules: {
        key: [
          {
            required: true,
            message: "数据字段不能为空",
            trigger: "change",
          },
        ],
        value: [
          {
            required: true,
            message: "请输入隐藏域值",
            trigger: "blur",
          },
        ],
      },
      FormData: {
        key: "",
        value: "",
        keytype: "",
      },
      showVisible: false,
      callback: () => {},
      myEntitymodelList: [],
      config: null,
    };
  },

  methods: {
    changeCodeType() {
      this.FormData.key = "";
      this.FormData.value = "";
    },
    changeValue(code) {
      this.FormData.value = "";
      this.config = this.myEntitymodelList.find((i) => i.code === code) || null;
      this.FormData.keytype = this.config?.type;
    },
    editForm(FormData = {}, data) {
      this.FormData = cloneDeep(FormData);
      this.config =
        data.myEntitymodelList.find((i) => i.code === FormData.key) || null;
      this.codeType = this.config ? "1" : "2";
      return this.showModel(data);
    },
    addForm(data) {
      this.FormData.key = "";
      this.FormData.value = "";
      this.config = null;
      this.codeType = "1";
      return this.showModel(data);
    },
    showModel({ myEntitymodelList, tagName }) {
      this.showVisible = true;
      this.loading = false;
      this.myEntitymodelList = myEntitymodelList;
      this.tagName = tagName;
      this.$nextTick(() => {
        this.$refs.Form.clearValidate();
      });
      return {
        then: (callback) => {
          if (typeof callback === "function") {
            this.callback = callback;
          }
        },
      };
    },
    async submit() {
      try {
        await this.$refs.Form.validate();
        this.loading = true;
        this.showVisible = false;
        this.callback(cloneDeep(this.FormData));
      } catch (error) {}
    },
  },
};
</script>

<style lang="scss" scoped>
.max {
  width: 220px;
}
</style>
